import M from '../../general/utils';

/**
 * 交叠对记录器
 */
export default class OverlapPairs{
    constructor(){
        this._init();
    }

    /**
     * 获取交叠对列表
     * @returns {*|Int16Array}
     */
    get pairs(){
        return this._pairs;
    }

    /**
     * 添加交叠对
     * @param nodeIndexA
     * @param nodeIndexB
     */
    addPair(nodeIndexA,nodeIndexB){
        for(let i=0;i<this._pairs.length;i+=2){
            if(this._pairs[i]<0){
                this._pairs[i]=nodeIndexA;
                this._pairs[i+1]=nodeIndexB;
                break;
            }
        }
        this._breakPoint+=2;
        this._pairs[this._breakPoint]=-1;
        this._checkVolume();
    }

    /**
     * 清除交叠对列表
     */
    clear(){
        this._breakPoint=0;
        this._pairs[this._breakPoint]=-1;
    }

    /**
     * 检查列表容量是否足够
     * @private
     */
    _checkVolume(){
        if(this._breakPoint>this._pairs.length-4){
            this._increaseVolume();
        }
    }

    /**
     * 增加列表容量
     * @private
     */
    _increaseVolume(){
        this._volume*=2;
        this._pairs=M.resizeTypedArray(this._pairs,this._volume);
    }

    _init(){
        this._initVariable();
    }
    _initVariable(){
        this._volume=64;
        this._breakPoint=0;
        this._pairs=new Int16Array(this._volume);//-1:断点,>=0:交叠对
        this._breakPoint=0;
        this._pairs[0]=-1;
    }
}